package com.dongjiantong.web.protal.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;

import com.dongjiantong.common.web.util.UserUtil;

import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.SysArea;
import com.dongjiantong.sys.model.SysLabelModel;
import com.dongjiantong.sys.service.SysAreaService;
import com.dongjiantong.user.model.RegionAccount;
import com.dongjiantong.user.service.OrgUserAreaService;
import com.dongjiantong.user.service.RegionAccountService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by cao on 2017/11/10.
 */
//@RestController
//@RequestMapping("/api/web/area")
@Slf4j
@OpenBean
public class AreaController {


    @Reference(version = "1.0.0")
    public RegionAccountService regionAccountService;

    @Reference(version = "1.0.0")
    public SysAreaService sysAreaService;

    @Reference(version = "1.0.0")
    public OrgUserAreaService orgUserAreaService;

    //@GetMapping("/provinceList")
    @OpenMethod(
            key = "web.area.provinceList",
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getProvinceList() {
        List<SysArea> list = sysAreaService.getProvinceList();

        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {

            labelModel = new SysLabelModel();

            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getProvince_id());
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);
    }

    @OpenMethod(
            key = "web.area.cityList",
            paramNames = {"provinceId"},
            httpMethods = RequestMethod.GET
    )

    public Response<List<SysLabelModel>> getCityList(Integer provinceId) {
        List<SysArea> list = sysAreaService.getCityList(provinceId);
        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {
            labelModel = new SysLabelModel();
            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getCity_id());
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);


    }

    @OpenMethod(
            key = "web.area.regionList",
            paramNames = {"provinceId", "cityId"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getRegionList(Integer provinceId, Integer cityId) {
        List<SysArea> list = sysAreaService.getRegionList(provinceId, cityId);
        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {
            labelModel = new SysLabelModel();
            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getRegion_id());
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);

    }

    @OpenMethod(
            key = "web.area.streetList",
            paramNames = {"provinceId", "cityId", "regionId"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getStreetList(Integer provinceId, Integer cityId, Integer regionId) {

        List<SysArea> list = sysAreaService.getStreetList(provinceId, cityId, regionId);
        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {

            labelModel = new SysLabelModel();

            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getStreet_id());
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);


    }

    @OpenMethod(
            key = "web.area.villageList",
            paramNames = {"provinceId", "cityId", "regionId", "street"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getVillageList(Integer provinceId, Integer cityId, Integer regionId, Integer street) {

        List<SysArea> list = sysAreaService.getVillageList(provinceId, cityId, regionId, street);
        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {

            labelModel = new SysLabelModel();

            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getStreet_id());
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);


    }

    /**
     * 查询登录人负责的区域树形
     *
     * @return
     */


    @OpenMethod(key = "web.area.GovUserArea",
            paramNames = {},
            httpMethods = RequestMethod.GET)
    public Response<List<Object>> queryLocationArea() {
        LoginInfo loginInfo = UserUtil.getCurrentUser();
        List list = orgUserAreaService.queryLocationArea(loginInfo.getId());

        return Response.ok(list);
    }

    @OpenMethod(key = "web.area.GovUserAreaByUserId",
            paramNames = {"userId"},
            httpMethods = RequestMethod.GET)
    public Response<List<Object>> queryLocationArea(Long userId) {

        List list = orgUserAreaService.queryLocationArea(userId);

        return Response.ok(list);
    }

    private List<Object> treeNode(long pid) {
        List<RegionAccount> childTreeNodes = regionAccountService.queryTreeNode(pid);

        List<Object> node = Lists.newArrayList();

        if (childTreeNodes.size() > 0) {
            for (RegionAccount ra : childTreeNodes) {

                Map<String, Object> childArray = Maps.newHashMap();

                if (ra.getParentId() == pid) {
                    childArray.put("label", ra.getAreaName());
                    childArray.put("value", ra.getAreaCode());

                    if (ra.getLevel() == 4) {
                        childArray.put("children", queryVillage(ra.getProvinceId(), ra.getCityId(), ra.getRegionId(), ra.getStreetId()));
                    } else {
                        childArray.put("children", treeNode(ra.getId()));
                    }
                    node.add(childArray);
                }

            }

        }
        return node;
    }

    private List<Object> queryVillage(Integer province_id, Integer city_id, Integer region_id, Integer street_id) {
        List<SysArea> list = sysAreaService.getVillageList(province_id, city_id, region_id, street_id);

        List<Object> node = Lists.newArrayList();

        if (list.size() > 0) {
            for (SysArea ra : list) {

                Map<String, Object> childArray = Maps.newHashMap();
                childArray.put("label", ra.getArea_name());
                childArray.put("value", ra.getVillage_id());
                node.add(childArray);
            }
        }
        return node;
    }


}
